<template>
    <ui-main>
        <van-search placeholder="请输入企业名称搜索" slot="header" v-model.trim="keyword" :disabled="loading" showAction>
            <van-loading size="20px" slot="action" v-if="loading"/>
            <div slot="action" @click="onSearch" v-else>搜索</div>
        </van-search>

        <ui-pull @load="getListData" v-model="listData" :total="total" :num.sync="pageNumber" ref="pull">
            <van-cell v-for="(item,index) of listData" :key="index" @click="detail(item)" :title="item.name" is-link></van-cell>
        </ui-pull>
    </ui-main>
</template>

<script>
    import cy from '../mixins/cy';

    export default {
        mixins: [cy],
        data() {
            return {
                // 数据源
                listData: [],
                // 其他
                // 搜索关键字
                keyword: '',
                // 页数
                pageNumber: 1,
                // 页码
                pageSize: 20,
                // 总记录数
                total: 0,
                // 是否正在加载：true-是；false-否
                loading: false
            };
        },
        watch: {
            keyword() {
                if (!this.keyword) {
                    this.onSearch();
                }
            }
        },
        mounted() {
            // 政府
            if (this.$user.data_type === '1') {
                this.$title('企业查询');

                // 企业直接跳转至明细页面
            } else {
                this.$router.replace({
                    path: '/qycxmx2',
                    query: {
                        from: 2,
                        id: this.$user.customer_id,
                        name: this.$user.customer_name,
                        data_type: this.$user.data_type
                    }
                });
            }
        },
        methods: {
            getListData({isDownScrolling}) {
                if (this.$user.data_type !== '1') {
                    this.$refs.pull && this.$refs.pull.endSuccess();

                    return;
                }

                this.loading = true;

                this.$$getAllEn({
                    county_no: this.$user.county_no,
                    keyword: this.keyword,
                    pageNumber: this.pageNumber,
                    pageSize: this.pageSize
                }).then(data => {
                    this.pageNumber++;
                    this.total = data.total;

                    if (isDownScrolling) {
                        this.listData = data.content;
                    } else {
                        this.listData = [...this.listData, ...data.content];
                    }

                    this.$refs.pull && this.$refs.pull.endSuccess();
                    this.loading = false;
                }).catch(e => {
                    console.error(e);

                    this.$refs.pull && this.$refs.pull.endSuccess();
                    this.loading = false;
                });
            },
            onSearch() {
                this.$refs.pull && this.$refs.pull.reload();
            },
            detail(item) {
                this.$router.push({
                    path: '/qycxmx1',
                    query: {
                        from: 1,
                        id: item.id,
                        name: item.name,
                        data_type: item.data_type
                    }
                });
            }
        }
    }
</script>
